package jnpf.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.service.VisualdevService;
import jnpf.base.util.DynDicUtil;
import jnpf.base.util.VisualUtils;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.entity.ZXGL_RenWuBianGengEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.generater.util.GeneraterSwapUtil;
import jnpf.model.zxgl_renwubiangeng.*;
import jnpf.service.ZXGL_RenWuBianGengService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.service.ZXGL_XiangMuLiXiang_MingXiService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * ZXGL_RenWuBianGeng
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-22
 */
@Slf4j
@RestController
@Api(tags = "任务变更", value = "zxgl")
@RequestMapping("/ZXGL_RenWuBianGeng")
public class ZXGL_RenWuBianGengController {


    @Autowired
    private DynDicUtil dynDicUtil;

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_RenWuBianGengService zXGL_RenWuBianGengService;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zxglXiangMuLiXiangMingXiService;

    /**
     * 列表
     *
     * @param zXGL_RenWuBianGengPagination
     * @return
     */
    @ApiOperation("任务变更分页")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_RenWuBianGengPagination zXGL_RenWuBianGengPagination) throws IOException {
        List<ZXGL_RenWuBianGengEntity> list = zXGL_RenWuBianGengService.getList(zXGL_RenWuBianGengPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_RenWuBianGengEntity entity : list) {
            entity.setSqr(generaterSwapUtil.userSelectValues(entity.getSqr()));
            entity.setSqbm(generaterSwapUtil.comSelectValues(entity.getSqbm()));
            entity.setBglx(dynDicUtil.getDicName(entity.getBglx(), "672043656068232837"));
            //entity.setXmrwlx(dynDicUtil.getDicName(entity.getXmrwlx(),"670160918163781253"));
            entity.setXmrwlx(dynDicUtil.getDicName(entity.getXmrwlx(), "667277254828496197"));

            entity.setBgqrwzt(dynDicUtil.getDicName(entity.getBgqrwzt(), "667276146122954053"));
            entity.setBghrwzt(dynDicUtil.getDicName(entity.getBghrwzt(), "667276146122954053"));
        }
        List<ZXGL_RenWuBianGengListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_RenWuBianGengListVO.class);
        for (ZXGL_RenWuBianGengListVO zXGL_RenWuBianGengVO : listVO) {
        }


        for (ZXGL_RenWuBianGengListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_RenWuBianGengPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_RenWuBianGengCrForm
     * @return
     */
    @ApiOperation("保存任务变更信息")
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_RenWuBianGengCrForm zXGL_RenWuBianGengCrForm) throws DataException {
        String visualId = "667624313473473733";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();

        // region 赋值项目责任人
        if (StrUtil.isNotEmpty(zXGL_RenWuBianGengCrForm.getXmid())) {
            ZXGL_XiangMuLiXiang_MingXiEntity mingXiEntity = zxglXiangMuLiXiangMingXiService.getById(zXGL_RenWuBianGengCrForm.getXmid());
            if (mingXiEntity != null) {
                zXGL_RenWuBianGengCrForm.setXmzrr(mingXiEntity.getZzr());
            }
        }
        // endregion

        // entity.setCreatorTime(new Date());

        // region 赋值部门负责人和领导
        ZXGL_XiangMuFenJie_mingxibiaoEntity model = zxglXiangMuFenJieMingxibiaoService.getInfo(zXGL_RenWuBianGengCrForm.getRwid());
        zXGL_RenWuBianGengCrForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
        zXGL_RenWuBianGengCrForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
        // endregion

        ZXGL_RenWuBianGengEntity entity = JsonUtil.getJsonToBean(zXGL_RenWuBianGengCrForm, ZXGL_RenWuBianGengEntity.class);
        entity.setId(mainId);



        zXGL_RenWuBianGengService.save(entity);
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_RenWuBianGengCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_RenWuBianGengCrForm, userInfo, zXGL_RenWuBianGengCrForm.getCandidateList());
                FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(mainId);
                if (flowTaskEntity != null) {
                    flowTaskEntity.setTitle(zXGL_RenWuBianGengCrForm.getRwmc() + "-任务变更-" + userInfo.getUserName() + "-" + DateUtil.today());
                    flowTaskEntity.setDataTable("zxgl_renwuBiangeng");
                    flowTaskService.update(flowTaskEntity);
                }
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("获取任务变更信息")
    @GetMapping("/{id}")
    public ActionResult<ZXGL_RenWuBianGengInfoVO> info(@PathVariable("id") String id) {
        ZXGL_RenWuBianGengEntity entity = zXGL_RenWuBianGengService.getInfo(id);
        ZXGL_RenWuBianGengInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_RenWuBianGengInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @ApiOperation("修改任务变更信息")
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_RenWuBianGengUpForm zXGL_RenWuBianGengUpForm) throws DataException {
        String visualId = "667624313473473733";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_RenWuBianGengEntity entity = zXGL_RenWuBianGengService.getInfo(id);
        if (entity != null) {
            // region 赋值项目责任人
            if (StrUtil.isNotEmpty(zXGL_RenWuBianGengUpForm.getXmid())) {
                ZXGL_XiangMuLiXiang_MingXiEntity mingXiEntity = zxglXiangMuLiXiangMingXiService.getById(zXGL_RenWuBianGengUpForm.getXmid());
                if (mingXiEntity != null) {
                    zXGL_RenWuBianGengUpForm.setXmzrr(mingXiEntity.getZzr());
                }
            }
            // endregion

            // region 赋值部门负责人和领导
            ZXGL_XiangMuFenJie_mingxibiaoEntity model = zxglXiangMuFenJieMingxibiaoService.getInfo(zXGL_RenWuBianGengUpForm.getRwid());
            zXGL_RenWuBianGengUpForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
            zXGL_RenWuBianGengUpForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
            // endregion

            ZXGL_RenWuBianGengEntity subentity = JsonUtil.getJsonToBean(zXGL_RenWuBianGengUpForm, ZXGL_RenWuBianGengEntity.class);

            zXGL_RenWuBianGengService.update(id, subentity);

            if (FlowStatusEnum.submit.getMessage().equals(zXGL_RenWuBianGengUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_RenWuBianGengUpForm.getCandidateList());
                    FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(id);
                    if (flowTaskEntity != null) {
                        flowTaskEntity.setTitle(zXGL_RenWuBianGengUpForm.getRwmc() + "-任务变更-" + userInfo.getUserName() + "-" + DateUtil.today());
                        flowTaskEntity.setDataTable("zxgl_renwuBiangeng");
                        flowTaskService.update(flowTaskEntity);
                    }
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除任务变更信息")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_RenWuBianGengEntity entity = zXGL_RenWuBianGengService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_RenWuBianGengService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


    /**
     * 流程更新任务变更
     *
     * @param zxglRenWuBianGengEntity
     * @return
     */
    @ApiOperation("流程更新任务变更")
    @PostMapping("/updateFlow")
    @Transactional
    public ActionResult updateLX(@RequestBody ZXGL_RenWuBianGengInfoVO zxglRenWuBianGengEntity) throws DataException {
        boolean update = zXGL_RenWuBianGengService.updateFlow(zxglRenWuBianGengEntity.getTaskId());
        if (update) {
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

}
